package com.welike.shibo.utils;

import com.welike.shibo.eneity.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author fangyu
 * @version v1.0.0
 * @since 2019-08-20 17:40
 */
public class SessionUtils {

    /**
     * 获取登录用户信息
     * @return
     */
    public static User getLoginUser(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return (User) request.getSession().getAttribute("user");
    }

    /**
     * 设置session用户信息
     */
    public static void setLoginUser(User user){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        request.getSession().setAttribute("user", user);
    }

}
